﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using com.feelgoodsoftware.sharptftp;

namespace ServerDemo
{
    public partial class ServerForm : Form
    {
        // the tftp server object.
        TftpServer _server = new TftpServer();

        // delegate for cross thread operation
        public delegate void InvokeDelegate(string Text);

        /// <summary>
        /// Constructor.
        /// </summary>
        public ServerForm()
        {
            InitializeComponent();
            DebugTextbox.Text = "ServerDemo started...\r\n";
        }

        private void ServerForm_Load(object sender, EventArgs e)
        {
            // setup the events for the server
            _server.OnFileRequest += new OnFileRequestDelegate(_server_OnFileRequest);
            _server.OnFileReceived += new OnFileReceivedDelegate(_server_OnFileReceived);
            _server.OnClientError += new OnClientErrorDelegate(_server_OnClientError);
            _server.OnClientTimeout += new OnClientTimeoutDelegate(_server_OnClientTimeout);
        }

        void _server_OnClientTimeout(object Sender, TftpClientRequest Request)
        {
            DebugTextbox.BeginInvoke(new InvokeDelegate(InvokeMethod), "Client timed out: " + Request.ToString() + "\r\n");
        }

        void _server_OnClientError(object Sender, TftpClientRequest Request, TftpErrorCode Code, string ErrorMessage)
        {
            DebugTextbox.BeginInvoke(new InvokeDelegate(InvokeMethod), "Client error: " + Request.ToString() + ", " + Code + ", " + ErrorMessage + "\r\n");
        }

        void _server_OnFileReceived(object Sender, TftpClientRequest Request, string Filename, byte[] Data)
        {
            // write the recieved bytes to the server (this goes in bin folder in project), NOTE: there is no error checking here (i.e. disk access/securtity).
            File.WriteAllBytes(Filename, Data);

            DebugTextbox.BeginInvoke(new InvokeDelegate(InvokeMethod), "File received from " + Request.ToString() + "\r\n");
        }

        void _server_OnFileRequest(object Sender, TftpClientRequest Request, string Filename)
        {
            DebugTextbox.BeginInvoke(new InvokeDelegate(InvokeMethod), "File: " + Application.StartupPath + "\\" + Filename + " requested from " + Request.ToString() + "\r\n");

            // send the requested data back, NOTE: there is no error checking here (i.e. disk access/securtity).
            if (File.Exists(Application.StartupPath + "\\" + Filename))
            {
                byte[] data = File.ReadAllBytes(Application.StartupPath + "\\" + Filename);
                _server.SendFileDataToClient(data, Request);
            }
            else
            {
                DebugTextbox.BeginInvoke(new InvokeDelegate(InvokeMethod), "File: " + Application.StartupPath + "\\" + Filename + " does not exist!\r\n");
                _server.SendErrorToClient(TftpErrorCode.FileNotFound, "File does not exist!", Request);
            }
        }

        /// <summary>
        /// Cross thread invoke delegate.
        /// </summary>
        /// <param name="Text">The text to display in the text box.</param>
        public void InvokeMethod(string Text)
        {
            DebugTextbox.Text += Text;
        }
    }
}
